﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZenTaoToFeiShu.Model;

namespace ZenTaoToFeiShu.Utils
{
    /// <summary>
    /// Config Utils
    /// </summary>
    public class Configs
    {
        /// <summary>
        /// Read all the configuration files
        /// </summary>
        /// <returns></returns>
        public static ConfigModel GetConfig()
        {
            try
            {
                StreamReader file = File.OpenText("config.json");
                JsonTextReader reader = new JsonTextReader(file);
                JObject jsonObject = (JObject)JToken.ReadFrom(reader);

                ConfigModel model = jsonObject.ToObject<ConfigModel>();
                file.Close();
                return model;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Read the configuration file
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfig(string key)
        {
            try
            {
                StreamReader file = File.OpenText("config.json");
                JsonTextReader reader = new JsonTextReader(file);
                JObject jsonObject = (JObject)JToken.ReadFrom(reader);

                string val = jsonObject[key].ToString();
                file.Close();
                return val;
            }
            catch
            {
                throw new Exception("Read configuration file exception");
            }
        }

        /// <summary>
        /// Modify the configuration file
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool EditConfig(string key, string val)
        {
            try
            {
                string json = File.ReadAllText("config.json");
                dynamic jsonObj = JsonConvert.DeserializeObject(json);
                jsonObj[key] = val;
                string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
                File.WriteAllText("config.json", output);
                return true;
            }
            catch
            {
                throw new Exception("Read configuration file exception");
            }
        }

        /// <summary>
        /// Modify the configuration file
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool EditConfig(ConfigModel model)
        {
            try
            {
                string json = File.ReadAllText("config.json");
                string output = JsonConvert.SerializeObject(model);
                File.WriteAllText("config.json", output);
                return true;
            }
            catch
            {
                throw new Exception("Read configuration file exception");
            }
        }
    }
}
